package ch10.shili;

public class S07_JoinThread implements Runnable{

    private int time = 1000;

    public S07_JoinThread(){

    }

    public S07_JoinThread(int time){
        this.time = time;
    }

    @Override
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName()+"开始摘香蕉");
        for(int i = 0; i < 10; i++){
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t.getName()+" 进度: 第" + (i+1)+"棵");
        }
        System.out.println(t.getName()+"已经完成采摘任务");
    }
}

class S07_Test{
    public static void main(String[] args) {
        Thread t1 = new Thread(new S07_JoinThread(1000),"果农A");
        Thread t2 = new Thread(new S07_JoinThread(1500),"果农B");
        t1.start();
        t2.start();
        for(int i = 0; i < 10; i++){
            try {
                if(i == 8){
                    System.out.println("果农main 先停下来");
                    t1.join();//强制先完成t1线程
                    t2.join();
                }
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("果农main 进度:"+" 进度: 第" + (i+1)+"棵");
        }
        System.out.println("果农main 完成任务");
    }
}